package net.killerscape.model.player;

import net.killerscape.model.Player;

public class Inventory {
	private Player player;
	public static final int FRAME = 3214, MAX_ITEMS = 28;
    public static final int SHOP_FRAME = 3823;
    public static final int BANK_FRAME = 5292; //5064
    public static final int TRADE_FRAME = 3322;
    private int frameInUse = FRAME;
	private InventoryItem[] inventory = new InventoryItem[MAX_ITEMS];

	public void setInventoryType(int type) {
		frameInUse = type;
		player.getActionSender().sendSideBarInterface(3, type);
		player.getActionSender().sendItems(itemIDs(), itemAmounts(), type);
	}
	
	public boolean playerHasItemInSlot(int id, int slot) {
		if(inventory[slot].getID() == id)
			return true;
		return false;
	}
	
	public int getFrameInUse() {
		return frameInUse;
	}
	
	public int[] itemIDs() {
		int[] output = new int[MAX_ITEMS];
		for(int i = 0; i < MAX_ITEMS; i++)
			if(inventory[i] == null)
				output[i] = -1;
			else
				output[i] = inventory[i].getID();
		return output;
	}
	
	public int[] itemAmounts() {
		int[] output = new int[MAX_ITEMS];
		for(int i = 0; i < MAX_ITEMS; i++)
			if(inventory[i] == null)
				output[i] = -1;
			else
				output[i] = inventory[i].getAmount();
		return output;
	}
	
	public Inventory(Player p) {
		player = p;
	}
	
	public void resetScreen() {
		player.getActionSender().clearItemsOnInterface(frameInUse);
	}
	
	public int getId(int slot) {
		if(!itemInSlot(slot))
			return -1;
		return inventory[slot].getID();
	}
	
	public int getAmount(int slot) {
		if(!itemInSlot(slot))
			return -1;
		return inventory[slot].getAmount();
	}
	
	public boolean itemInSlot(int slot) {
		return inventory[slot] != null;
	}
	
	public int addItem(int id, int amount) {
		int slot = -1;
		for(int i = 0; i < MAX_ITEMS; i++)
			if(!itemInSlot(i)) {
				slot = i;
				break;
			}
		if(slot == -1) return slot;
		inventory[slot] = new InventoryItem(id,amount);
		player.getActionSender().sendItem(id, amount, slot, frameInUse);
		return slot;
	}
	
	public boolean removeItemAtSlot(int slot) {
		if(!itemInSlot(slot))
			return false;
		inventory[slot] = null;
		player.getActionSender().sendItem(-1,-1,slot,frameInUse);
		return true;
	}
	
	public boolean removeItem(int id) {
		for(int i = 0; i < MAX_ITEMS; i++)
			if(itemInSlot(i))
				if(getId(i) == id) {
					removeItemAtSlot(i);
					return true;
				}
		return false;
	}
	
	public boolean removeXItem(int id, int amount) {
		
		return false;
	}
	
	public boolean playerHasItem(int id) {
		return playerHasItemAmount(id, 1);
	}
	
	public boolean playerHasItemAmount(int id, int amount) {
		int amtFound = 0;
		for(int i = 0; i < MAX_ITEMS; i++)
			if(itemInSlot(i))
				if(getId(i) == id) {
					amtFound += getAmount(i);
					if(amtFound >= amount)
						return true;
					else
						continue;
				}
		return false;
	}
	
	static class InventoryItem {
		private int itemID,itemAmount;
		
		public InventoryItem(int id, int amount) {
			itemID = id;
			itemAmount = amount;
		}
		
		public int getID() {
			return itemID;
		}
		
		public void setID(int id) {
			itemID = id;
		}
		
		public int getAmount() {
			return itemAmount;
		}
		
		public void setAmount(int amount) {
			itemAmount = amount;
		}
	}
}
